<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Generator" content="Kate, the KDE Advanced Text Editor" />
<title>tutorial1.py</title>
</head>
<body>
<pre style='color:#141312;background-color:#ffffff;'>
<i><span style='color:#008000;'>#!/usr/bin/env python</span></i>

<i><span style='color:#008000;'>&quot;&quot;&quot;********************************************************************************</span></i>
<i><span style='color:#008000;'>                             tutorial1.py</span></i>
<i><span style='color:#008000;'>                 DAE Tools: pyDAE module, www.daetools.com</span></i>
<i><span style='color:#008000;'>                 Copyright (C) Dragan Nikolic, 2010</span></i>
<i><span style='color:#008000;'>***********************************************************************************</span></i>
<i><span style='color:#008000;'>DAE Tools is free software; you can redistribute it and/or modify it under the </span></i>
<i><span style='color:#008000;'>terms of the GNU General Public License as published by the Free Software </span></i>
<i><span style='color:#008000;'>Foundation; either version 3 of the License, or (at your option) any later version.</span></i>
<i><span style='color:#008000;'>The DAE Tools is distributed in the hope that it will be useful, but WITHOUT ANY </span></i>
<i><span style='color:#008000;'>WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A </span></i>
<i><span style='color:#008000;'>PARTICULAR PURPOSE. See the GNU General Public License for more details.</span></i>
<i><span style='color:#008000;'>You should have received a copy of the GNU General Public License along with the</span></i>
<i><span style='color:#008000;'>DAE Tools software; if not, write to the Free Software Foundation, Inc., </span></i>
<i><span style='color:#008000;'>59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.</span></i>
<i><span style='color:#008000;'>********************************************************************************&quot;&quot;&quot;</span></i>

<i><span style='color:#008000;'>&quot;&quot;&quot;</span></i>
<i><span style='color:#008000;'>This tutorial introduces several new concepts:</span></i>
<i><span style='color:#008000;'> - Distribution domains</span></i>
<i><span style='color:#008000;'> - Distributed parameters, variables and equations</span></i>
<i><span style='color:#008000;'> - Boundary and initial conditions</span></i>

<i><span style='color:#008000;'>In this example we model a simple heat conduction problem: </span></i>
<i><span style='color:#008000;'>   - a conduction through a very thin, rectangular copper plate.</span></i>
<i><span style='color:#008000;'>This example should be sufficiently complex to describe all basic DAE Tools features. </span></i>
<i><span style='color:#008000;'>For this problem, we need a two-dimensional Cartesian grid in X and Y axis </span></i>
<i><span style='color:#008000;'>(here, for simplicity, divided into 10 x 10 segments):</span></i>

<i><span style='color:#008000;'>Y axis</span></i>
<i><span style='color:#008000;'>    ^</span></i>
<i><span style='color:#008000;'>    |</span></i>
<i><span style='color:#008000;'>Ly -| T T T T T T T T T T T</span></i>
<i><span style='color:#008000;'>    | L + + + + + + + + + R</span></i>
<i><span style='color:#008000;'>    | L + + + + + + + + + R </span></i>
<i><span style='color:#008000;'>    | L + + + + + + + + + R</span></i>
<i><span style='color:#008000;'>    | L + + + + + + + + + R</span></i>
<i><span style='color:#008000;'>    | L + + + + + + + + + R</span></i>
<i><span style='color:#008000;'>    | L + + + + + + + + + R</span></i>
<i><span style='color:#008000;'>    | L + + + + + + + + + R</span></i>
<i><span style='color:#008000;'>    | L + + + + + + + + + R</span></i>
<i><span style='color:#008000;'>    | L + + + + + + + + + R</span></i>
<i><span style='color:#008000;'> 0 -| B B B B B B B B B B B</span></i>
<i><span style='color:#008000;'>    --|-------------------|----&gt; X axis</span></i>
<i><span style='color:#008000;'>      0                   Lx</span></i>
<i><span style='color:#008000;'>     </span></i>
<i><span style='color:#008000;'>Points 'B' at the bottom edge of the plate (for y = 0), and the points 'T' at the top edge of the plate </span></i>
<i><span style='color:#008000;'>(for y = Ly) represent the points where the heat is applied.</span></i>
<i><span style='color:#008000;'>The plate is considered insulated at the left (x = 0) and the right edges (x = Lx) of the plate (points 'L' and 'R'). </span></i>
<i><span style='color:#008000;'>To model this type of problem, we have to write a heat balance equation for all interior points except the left, right, </span></i>
<i><span style='color:#008000;'>top and bottom edges, where we need to define the Neumann type boundary conditions. </span></i>

<i><span style='color:#008000;'>In this problem we have to define the following domains:</span></i>
<i><span style='color:#008000;'> - x: X axis domain, length Lx = 0.1 m </span></i>
<i><span style='color:#008000;'> - y: Y axis domain, length Ly = 0.1 m </span></i>

<i><span style='color:#008000;'>the following parameters:</span></i>
<i><span style='color:#008000;'> - ro: copper density, 8960 kg/m3</span></i>
<i><span style='color:#008000;'> - cp: copper specific heat capacity, 385 J/(kgK)</span></i>
<i><span style='color:#008000;'> - k:  copper heat conductivity, 401 W/(mK)</span></i>
<i><span style='color:#008000;'> - Qb: heat flux at the bottom edge of the plate, 1E6 W/m2 (or 100 W/cm2)</span></i>
<i><span style='color:#008000;'> - Qt: heat flux at the top edge of the plate, here set to 0 W/m2</span></i>
<i><span style='color:#008000;'> </span></i>
<i><span style='color:#008000;'>and the following variable:</span></i>
<i><span style='color:#008000;'> - T: the temperature of the plate, K (distributed on x and y domains)</span></i>

<i><span style='color:#008000;'>Also, we need to write the following 5 equations:</span></i>

<i><span style='color:#008000;'>1) Heat balance:</span></i>
<i><span style='color:#008000;'>      ro * cp * dT(x,y) / dt = k * (d2T(x,y) / dx2 + d2T(x,y) / dy2);  for all x in: (0, Lx),</span></i>
<i><span style='color:#008000;'>                                                                       for all y in: (0, Ly) </span></i>

<i><span style='color:#008000;'>2) Boundary conditions for the bottom edge:</span></i>
<i><span style='color:#008000;'>      -k * dT(x,y) / dy = Qin;  for all x in: [0, Lx],</span></i>
<i><span style='color:#008000;'>                                and y = 0</span></i>

<i><span style='color:#008000;'>3) Boundary conditions for the top edge:</span></i>
<i><span style='color:#008000;'>      -k * dT(x,y) / dy = Qin;  for all x in: [0, Lx],</span></i>
<i><span style='color:#008000;'>                                and y = Ly</span></i>

<i><span style='color:#008000;'>4) Boundary conditions for the left edge:</span></i>
<i><span style='color:#008000;'>      dT(x,y) / dx = 0;  for all y in: (0, Ly),</span></i>
<i><span style='color:#008000;'>                         and x = 0</span></i>

<i><span style='color:#008000;'>5) Boundary conditions for the right edge:</span></i>
<i><span style='color:#008000;'>      dT(x,y) / dx = 0;  for all y in: (0, Ly),</span></i>
<i><span style='color:#008000;'>                         and x = Ln</span></i>

<i><span style='color:#008000;'>&quot;&quot;&quot;</span></i>

<span style='color:#0000ff;'>import</span> sys
<span style='color:#0000ff;'>from</span> daetools.pyDAE <span style='color:#0000ff;'>import</span> <span style='color:#0000ff;'>*</span>
<span style='color:#0000ff;'>from</span> time <span style='color:#0000ff;'>import</span> localtime, strftime

typeNone         <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;None&quot;</span>,         <span style='color:#bf0303;'>&quot;-&quot;</span>,      <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>1E10</span>,   <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>1e-5</span>)
typeTemperature  <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;Temperature&quot;</span>,  <span style='color:#bf0303;'>&quot;K&quot;</span>,    <span style='color:#c000c0;'>100</span>, <span style='color:#c000c0;'>1000</span>, <span style='color:#c000c0;'>300</span>, <span style='color:#c000c0;'>1e-5</span>)
typeConductivity <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;Conductivity&quot;</span>, <span style='color:#bf0303;'>&quot;W/mK&quot;</span>,   <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>1E10</span>, <span style='color:#c000c0;'>100</span>, <span style='color:#c000c0;'>1e-5</span>)
typeDensity      <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;Density&quot;</span>,      <span style='color:#bf0303;'>&quot;kg/m3&quot;</span>,  <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>1E10</span>, <span style='color:#c000c0;'>100</span>, <span style='color:#c000c0;'>1e-5</span>)
typeHeatCapacity <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;HeatCapacity&quot;</span>, <span style='color:#bf0303;'>&quot;J/KgK&quot;</span>,  <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>1E10</span>, <span style='color:#c000c0;'>100</span>, <span style='color:#c000c0;'>1e-5</span>)

<b>class</b> modTutorial(daeModel):
    <b>def</b> <b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>, Name, Parent <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>None</span>, Description <span style='color:#0000ff;'>=</span> <span style='color:#bf0303;'>&quot;&quot;</span>):
        daeModel.<b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>, Name, Parent, Description)
        
        <i><span style='color:#008000;'># Distribution domain is a general term used to define an array of different objects (parameters, variables, etc).</span></i>
        <i><span style='color:#008000;'># daeDomain constructor accepts three arguments:</span></i>
        <i><span style='color:#008000;'>#  - Name: string</span></i>
        <i><span style='color:#008000;'>#  - Parent: daeModel object (indicating the model where the domain will be added)</span></i>
        <i><span style='color:#008000;'>#  - Description: string (optional argument; the default value is an empty string)</span></i>
        <i><span style='color:#008000;'># All naming conventions (introduced in Whats_the_time example) apply here as well.</span></i>
        <i><span style='color:#008000;'># Again, domains have to be declared as members of the new model class (like all the other objects)</span></i>
        <span style='color:#0000ff;'>self</span>.x <span style='color:#0000ff;'>=</span> daeDomain(<span style='color:#bf0303;'>&quot;x&quot;</span>, <span style='color:#0000ff;'>self</span>, <span style='color:#bf0303;'>&quot;X axis domain&quot;</span>)
        <span style='color:#0000ff;'>self</span>.y <span style='color:#0000ff;'>=</span> daeDomain(<span style='color:#bf0303;'>&quot;y&quot;</span>, <span style='color:#0000ff;'>self</span>, <span style='color:#bf0303;'>&quot;Y axis domain&quot;</span>)

        <i><span style='color:#008000;'># Parameter can be defined as a time invariant quantity that will not change during a simulation.</span></i>
        <i><span style='color:#008000;'># daeParameter constructor accepts three arguments:</span></i>
        <i><span style='color:#008000;'>#  - Name: string</span></i>
        <i><span style='color:#008000;'>#  - Parent: daeModel object (indicating the model where the domain will be added)</span></i>
        <i><span style='color:#008000;'>#  - Description: string (optional argument; the default value is an empty string)</span></i>
        <i><span style='color:#008000;'># All naming conventions (introduced in whats_the_time example) apply here as well.</span></i>
        <span style='color:#0000ff;'>self</span>.Qb <span style='color:#0000ff;'>=</span> daeParameter(<span style='color:#bf0303;'>&quot;Q_b&quot;</span>,      eReal, <span style='color:#0000ff;'>self</span>, <span style='color:#bf0303;'>&quot;Heat flux at the bottom edge of the plate, W/m2&quot;</span>)
        <span style='color:#0000ff;'>self</span>.Qt <span style='color:#0000ff;'>=</span> daeParameter(<span style='color:#bf0303;'>&quot;Q_t&quot;</span>,      eReal, <span style='color:#0000ff;'>self</span>, <span style='color:#bf0303;'>&quot;Heat flux at the top edge of the plate, W/m2&quot;</span>)
        <span style='color:#0000ff;'>self</span>.ro <span style='color:#0000ff;'>=</span> daeParameter(<span style='color:#bf0303;'>&quot;&amp;rho;&quot;</span>,    eReal, <span style='color:#0000ff;'>self</span>, <span style='color:#bf0303;'>&quot;Density of the plate, kg/m3&quot;</span>)
        <span style='color:#0000ff;'>self</span>.cp <span style='color:#0000ff;'>=</span> daeParameter(<span style='color:#bf0303;'>&quot;c_p&quot;</span>,      eReal, <span style='color:#0000ff;'>self</span>, <span style='color:#bf0303;'>&quot;Specific heat capacity of the plate, J/kgK&quot;</span>)
        <span style='color:#0000ff;'>self</span>.k  <span style='color:#0000ff;'>=</span> daeParameter(<span style='color:#bf0303;'>&quot;&amp;lambda;&quot;</span>, eReal, <span style='color:#0000ff;'>self</span>, <span style='color:#bf0303;'>&quot;Thermal conductivity of the plate, W/mK&quot;</span>)

        <i><span style='color:#008000;'># In this example we need a variable T which is distributed on the domains x and y. Variables (but also other objects)</span></i>
        <i><span style='color:#008000;'># can be distributed by using a function DistributeOnDomain, which accepts a domain object as </span></i>
        <i><span style='color:#008000;'># the argument (previously declared in the constructor).</span></i>
        <i><span style='color:#008000;'># Also a description of the object can be set by using the property Description.</span></i>
        <span style='color:#0000ff;'>self</span>.T <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;T&quot;</span>, typeTemperature, <span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.T.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.x)
        <span style='color:#0000ff;'>self</span>.T.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.y)
        <span style='color:#0000ff;'>self</span>.T.Description <span style='color:#0000ff;'>=</span> <span style='color:#bf0303;'>&quot;Temperature of the plate, K&quot;</span>

    <b>def</b> DeclareEquations(<span style='color:#0000ff;'>self</span>):
        <i><span style='color:#008000;'># To distribute an equation on a domain the function DistributeOnDomain can be again used.</span></i>
        <i><span style='color:#008000;'># However, when distributing equations the function DistributeOnDomain takes an additional argument.</span></i>
        <i><span style='color:#008000;'># The second argument, DomainBounds, can be either of type daeeDomainBounds or a list of integers.</span></i>
        <i><span style='color:#008000;'># In the former case the DomainBounds argument is a flag defining a subset of the domain points.</span></i>
        <i><span style='color:#008000;'># There are several flags available:</span></i>
        <i><span style='color:#008000;'>#  - eClosedClosed: Distribute on a closed domain - analogous to: x: [ LB, UB ]</span></i>
        <i><span style='color:#008000;'>#  - eOpenClosed: Distribute on a left open domain - analogous to: x: ( LB, UB ]</span></i>
        <i><span style='color:#008000;'>#  - eClosedOpen: Distribute on a right open domain - analogous to: x: [ LB, UB )</span></i>
        <i><span style='color:#008000;'>#  - eOpenOpen: Distribute on a domain open on both sides - analogous to: x: ( LB, UB )</span></i>
        <i><span style='color:#008000;'>#  - eLowerBound: Distribute on the lower bound - only one point: x = LB</span></i>
        <i><span style='color:#008000;'>#    This option is useful for declaring boundary conditions.</span></i>
        <i><span style='color:#008000;'>#  - eUpperBound: Distribute on the upper bound - only one point: x = UB</span></i>
        <i><span style='color:#008000;'>#    This option is useful for declaring boundary conditions.</span></i>
        <i><span style='color:#008000;'># Also DomainBounds argument can be a list (an array) of points within a domain, for example: x: {0, 3, 4, 6, 8, 10}</span></i>
        <i><span style='color:#008000;'># Since our heat balance equation should exclude the top, bottom, left and right edges,</span></i>
        <i><span style='color:#008000;'># it is distributed on the open x and y domains, thus we use the eOpenOpen flag:</span></i>
        eq <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.CreateEquation(<span style='color:#bf0303;'>&quot;HeatBalance&quot;</span>, <span style='color:#bf0303;'>&quot;Heat balance equation. Valid on the open x and y domains&quot;</span>)
        x <span style='color:#0000ff;'>=</span> eq.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.x, eOpenOpen)
        y <span style='color:#0000ff;'>=</span> eq.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.y, eOpenOpen)
        eq.Residual <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.ro() <span style='color:#0000ff;'>*</span> <span style='color:#0000ff;'>self</span>.cp() <span style='color:#0000ff;'>*</span> <span style='color:#0000ff;'>self</span>.T.dt(x, y) <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.k() <span style='color:#0000ff;'>*</span> <span style='color:#0000ff;'>\</span>
                     (<span style='color:#0000ff;'>self</span>.T.d2(<span style='color:#0000ff;'>self</span>.x, x, y) <span style='color:#0000ff;'>+</span> <span style='color:#0000ff;'>self</span>.T.d2(<span style='color:#0000ff;'>self</span>.y, x, y))
        
        <i><span style='color:#008000;'># Boundary conditions are treated as ordinary equations, and the special eLowerBound and eUpperBound flags</span></i>
        <i><span style='color:#008000;'># are used to define the position of the boundary. </span></i>
        <i><span style='color:#008000;'># The bottom edge is placed at y = 0 coordinate, thus we use eLowerBound for the y domain:</span></i>
        eq <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.CreateEquation(<span style='color:#bf0303;'>&quot;BC_bottom&quot;</span>, <span style='color:#bf0303;'>&quot;Boundary conditions for the bottom edge&quot;</span>)
        x <span style='color:#0000ff;'>=</span> eq.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.x, eClosedClosed)
        y <span style='color:#0000ff;'>=</span> eq.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.y, eLowerBound)
        eq.Residual <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.k() <span style='color:#0000ff;'>*</span> <span style='color:#0000ff;'>self</span>.T.d(<span style='color:#0000ff;'>self</span>.y, x, y) <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.Qb()

        <i><span style='color:#008000;'># The top edge is placed at y = Ly coordinate, thus we use eUpperBound for the y domain:</span></i>
        eq <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.CreateEquation(<span style='color:#bf0303;'>&quot;BC_top&quot;</span>, <span style='color:#bf0303;'>&quot;Boundary conditions for the top edge&quot;</span>)
        x <span style='color:#0000ff;'>=</span> eq.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.x, eClosedClosed)
        y <span style='color:#0000ff;'>=</span> eq.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.y, eUpperBound)
        eq.Residual <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.k() <span style='color:#0000ff;'>*</span> <span style='color:#0000ff;'>self</span>.T.d(<span style='color:#0000ff;'>self</span>.y, x, y) <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.Qt()

        <i><span style='color:#008000;'># The left edge is placed at x = 0 coordinate, thus we use eLowerBound for the x domain:</span></i>
        eq <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.CreateEquation(<span style='color:#bf0303;'>&quot;BC_left&quot;</span>, <span style='color:#bf0303;'>&quot;Boundary conditions at the left edge&quot;</span>)
        x <span style='color:#0000ff;'>=</span> eq.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.x, eLowerBound)
        y <span style='color:#0000ff;'>=</span> eq.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.y, eOpenOpen)
        eq.Residual <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.T.d(<span style='color:#0000ff;'>self</span>.x, x, y)

        <i><span style='color:#008000;'># The right edge is placed at x = Lx coordinate, thus we use eUpperBound for the x domain:</span></i>
        eq <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.CreateEquation(<span style='color:#bf0303;'>&quot;BC_righ&quot;</span>, <span style='color:#bf0303;'>&quot;Boundary conditions for the right edge&quot;</span>)
        x <span style='color:#0000ff;'>=</span> eq.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.x, eUpperBound)
        y <span style='color:#0000ff;'>=</span> eq.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.y, eOpenOpen)
        eq.Residual <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.T.d(<span style='color:#0000ff;'>self</span>.x, x, y)

<b>class</b> simTutorial(daeDynamicSimulation):
    <b>def</b> <b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>):
        daeDynamicSimulation.<b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.m <span style='color:#0000ff;'>=</span> modTutorial(<span style='color:#bf0303;'>&quot;Tutorial_1&quot;</span>)
        <span style='color:#0000ff;'>self</span>.m.Description <span style='color:#0000ff;'>=</span> <span style='color:#bf0303;'>&quot;This tutorial explains how to define and set up domains, ordinary and distributed parameters &quot;</span> <span style='color:#0000ff;'>\</span>
                             <span style='color:#bf0303;'>&quot;and variables, how to define distributed domains, declare distributed equations and set &quot;</span> <span style='color:#0000ff;'>\</span>
                             <span style='color:#bf0303;'>&quot;their boundary and initial conditions.&quot;</span>
          
    <b>def</b> SetUpParametersAndDomains(<span style='color:#0000ff;'>self</span>):
        <i><span style='color:#008000;'># In this example we use the center-finite difference method (CFDM) of 2nd order to discretize the domains x and y.</span></i>
        <i><span style='color:#008000;'># The function CreateDistributed can be used to create a distributed domain. It accepts 5 arguments:</span></i>
        <i><span style='color:#008000;'># - DiscretizationMethod: can be eBFDM (backward-), BFDM (forward) and eCFDM (center) finite difference method</span></i>
        <i><span style='color:#008000;'># - Order: currently only 2nd order is implemented</span></i>
        <i><span style='color:#008000;'># - NoIntervals: 25</span></i>
        <i><span style='color:#008000;'># - LowerBound: 0</span></i>
        <i><span style='color:#008000;'># - UpperBound: 0.1</span></i>
        <i><span style='color:#008000;'># Here we use 25 intervals. In general any number of intervals can be used. However, the computational costs become </span></i>
        <i><span style='color:#008000;'># prohibitive at the very high number (especially if dense linear solvers are used).</span></i>
        <span style='color:#0000ff;'>self</span>.m.x.CreateDistributed(eCFDM, <span style='color:#c000c0;'>2</span>, <span style='color:#c000c0;'>25</span>, <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>0.1</span>)
        <span style='color:#0000ff;'>self</span>.m.y.CreateDistributed(eCFDM, <span style='color:#c000c0;'>2</span>, <span style='color:#c000c0;'>25</span>, <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>0.1</span>)

        <i><span style='color:#008000;'># Parameters' value can be set by using a function SetValue. </span></i>
        <span style='color:#0000ff;'>self</span>.m.k.SetValue(<span style='color:#c000c0;'>401</span>)
        <span style='color:#0000ff;'>self</span>.m.cp.SetValue(<span style='color:#c000c0;'>385</span>)
        <span style='color:#0000ff;'>self</span>.m.ro.SetValue(<span style='color:#c000c0;'>8960</span>)
        <span style='color:#0000ff;'>self</span>.m.Qb.SetValue(<span style='color:#c000c0;'>1e6</span>)
        <span style='color:#0000ff;'>self</span>.m.Qt.SetValue(<span style='color:#c000c0;'>0</span>)

    <b>def</b> SetUpVariables(<span style='color:#0000ff;'>self</span>):
        <i><span style='color:#008000;'># SetInitialCondition function in the case of distributed variables can accept additional arguments</span></i>
        <i><span style='color:#008000;'># specifying the indexes in the domains. In this example we loop over the open x and y domains, </span></i>
        <i><span style='color:#008000;'># thus we start the loop with 1 and end with NumberOfPoints-1 (for both domains) </span></i>
        <b>for</b> x <span style='color:#0000ff;'>in</span> <b><span style='color:#000000;'>range</span></b>(<span style='color:#c000c0;'>1</span>, <span style='color:#0000ff;'>self</span>.m.x.NumberOfPoints <span style='color:#0000ff;'>-</span> <span style='color:#c000c0;'>1</span>):
            <b>for</b> y <span style='color:#0000ff;'>in</span> <b><span style='color:#000000;'>range</span></b>(<span style='color:#c000c0;'>1</span>, <span style='color:#0000ff;'>self</span>.m.y.NumberOfPoints <span style='color:#0000ff;'>-</span> <span style='color:#c000c0;'>1</span>):
                <span style='color:#0000ff;'>self</span>.m.T.SetInitialCondition(x, y, <span style='color:#c000c0;'>300</span>)

<i><span style='color:#008000;'># Use daeSimulator class</span></i>
<b>def</b> guiRun():
    <span style='color:#0000ff;'>from</span> PyQt4 <span style='color:#0000ff;'>import</span> QtCore, QtGui
    app <span style='color:#0000ff;'>=</span> QtGui.QApplication(sys.argv)
    simulation <span style='color:#0000ff;'>=</span> simTutorial()
    simulation.m.SetReportingOn(<span style='color:#0000ff;'>True</span>)
    simulation.ReportingInterval <span style='color:#0000ff;'>=</span> <span style='color:#c000c0;'>10</span>
    simulation.TimeHorizon       <span style='color:#0000ff;'>=</span> <span style='color:#c000c0;'>1000</span>
    simulator  <span style='color:#0000ff;'>=</span> daeSimulator(app, simulation)
    simulator.show()
    app.exec_()

<i><span style='color:#008000;'># Setup everything manually and run in a console</span></i>
<b>def</b> consoleRun():
    <i><span style='color:#008000;'># Create Log, Solver, DataReporter and Simulation object</span></i>
    log          <span style='color:#0000ff;'>=</span> daePythonStdOutLog()
    solver       <span style='color:#0000ff;'>=</span> daeIDASolver()
    datareporter <span style='color:#0000ff;'>=</span> daeTCPIPDataReporter()
    simulation   <span style='color:#0000ff;'>=</span> simTutorial()

    <i><span style='color:#008000;'># Enable reporting of all variables</span></i>
    simulation.m.SetReportingOn(<span style='color:#0000ff;'>True</span>)

    <i><span style='color:#008000;'># Set the time horizon and the reporting interval</span></i>
    simulation.ReportingInterval <span style='color:#0000ff;'>=</span> <span style='color:#c000c0;'>10</span>
    simulation.TimeHorizon <span style='color:#0000ff;'>=</span> <span style='color:#c000c0;'>1000</span>

    <i><span style='color:#008000;'># Connect data reporter</span></i>
    simName <span style='color:#0000ff;'>=</span> simulation.m.Name <span style='color:#0000ff;'>+</span> strftime(<span style='color:#bf0303;'>&quot; [</span><span style='color:#0000ff;'>%d</span><span style='color:#bf0303;'>.%m.%Y %H:%M:%S]&quot;</span>, localtime())
    <b>if</b>(datareporter.Connect(<span style='color:#bf0303;'>&quot;&quot;</span>, simName) <span style='color:#0000ff;'>==</span> <span style='color:#0000ff;'>False</span>):
        sys.exit()

    <i><span style='color:#008000;'># Initialize the simulation</span></i>
    simulation.Initialize(solver, datareporter, log)

    <i><span style='color:#008000;'># Save the model report and the runtime model report </span></i>
    simulation.m.SaveModelReport(simulation.m.Name <span style='color:#0000ff;'>+</span> <span style='color:#bf0303;'>&quot;.xml&quot;</span>)
    simulation.m.SaveRuntimeModelReport(simulation.m.Name <span style='color:#0000ff;'>+</span> <span style='color:#bf0303;'>&quot;-rt.xml&quot;</span>)

    <i><span style='color:#008000;'># Solve at time=0 (initialization)</span></i>
    simulation.SolveInitial()

    <i><span style='color:#008000;'># Run</span></i>
    simulation.Run()

<b>if</b> <b><span style='color:#000000;'>__name__</span></b> <span style='color:#0000ff;'>==</span> <span style='color:#bf0303;'>&quot;__main__&quot;</span>:
    runInGUI <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>True</span>
    <b>if</b> <b><span style='color:#000000;'>len</span></b>(sys.argv) <span style='color:#0000ff;'>&gt;</span> <span style='color:#c000c0;'>1</span>:
        <b>if</b>(sys.argv[<span style='color:#c000c0;'>1</span>] <span style='color:#0000ff;'>==</span> <span style='color:#bf0303;'>'console'</span>):
            runInGUI <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>False</span>
    <b>if</b> runInGUI:
        guiRun()
    <b>else</b>:
        consoleRun()
</pre>
</body>
</html>
